package com.ruoyi.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.ruoyi.entity.AppVisit;
import com.ruoyi.service.AppVisitService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-10-22 12:15 AM
 */
@Aspect
@Component
public class cAdvice {
    @Autowired
    private AppVisitService appVisitService;
    private Logger logger = LoggerFactory.getLogger(cAdvice.class);

    //定义切面
    @Pointcut(value = "execution(* com.ruoyi.app.controller.*.*(..))||execution(* com.ruoyi.eduinfo.controller.api.*.*(..))")
    public void myPointcut(){


    }
    //
    @Around("myPointcut()")
    public Object myLogger(ProceedingJoinPoint pjp) throws Throwable {

        Class<?> targetClass=pjp.getTarget().getClass();
        Method targetMethod=((MethodSignature) pjp.getSignature()).getMethod();

        String className= targetClass.toString();

        String methodName= targetMethod.getName();


        Object[] array = pjp.getArgs();

        ObjectMapper mapper = new ObjectMapper();

        logger.info("调用前"+className+":"+methodName+"传递的参数为:"+mapper.writeValueAsString(array));
        System.out.println("cch");

        Object obj =pjp.proceed();

//        System.out.println(pjp.getSignature().getName());


        RequestMapping requestMapping =targetClass.getAnnotation(RequestMapping.class);

        String api=requestMapping.value()[0];
        GetMapping getMapping=
                targetMethod.getAnnotation(GetMapping.class);

        if (null!=getMapping){
            api+=getMapping.value()[0];
        }else {
            PostMapping postMapping =
                    targetMethod.getAnnotation(PostMapping.class);
            if (null != postMapping) {
                api+=postMapping.value()[0];
            } else {
                System.out.println("不是post和get");

            }
        }


        JSONObject jsonpObject=JSON.parseObject((mapper.writeValueAsString(obj)));
        if (200 == (Integer) jsonpObject.get("code")){
            AppVisit appVisit =new AppVisit();
            appVisit.setAppId((Integer) array[0]);
            appVisit.setMethod(className+":"+methodName);
            appVisit.setVisitUrl(api);
            appVisit.setUpdateTime(new Date());
            appVisitService.save(appVisit);
        }
        logger.info("调用后"+className+":"+methodName+"返回值为:"+mapper.writeValueAsString(obj)+"接口地址为："+api);


        return obj;
    }
}
